package com.noble.glamour.ui.discover;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.light.UserInfoLightweight;
import com.noble.glamour.bean.center.user.light.UserInfoLightweightList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.BaseFragment;
import com.noble.glamour.module.logic.baseui.custom.CustomStatusListView;
import com.noble.glamour.module.logic.baseui.xlistview.ExListView;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;

import java.util.ArrayList;
import java.util.List;

/**
 * 语聊
 * <p>
 * Created by Su on 2017/8/9.
 */
public class ChatFragment extends BaseFragment implements RequestComplete, ExListView.IXListViewListener {
    private CustomStatusListView customStatusListView;
    private ExListView exListView;

    private ChatFragmentAdapter adapter;
    private List<UserInfoLightweight> infos = new ArrayList<>();

    private int page = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.chat_fragment);

        initView();
        onRefresh(); //默认加载全部
        return getContentView();
    }

    private void initView() {
        customStatusListView = (CustomStatusListView) findViewById(R.id.chat_content);
        View mViewTop = LayoutInflater.from(getContext()).inflate(R.layout.layout_margintop, null);
        exListView = customStatusListView.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        exListView.addHeaderView(mViewTop);
        adapter = new ChatFragmentAdapter(getActivity(), infos);
        exListView.setAdapter(adapter);
        exListView.setHeaderStr(null, null);
        exListView.setHeaderHintType(2);
        customStatusListView.showLoading();
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getCommonMgr().getVideoChatUsers(page, 1, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getCommonMgr().getVideoChatUsers(page, 0, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        exListView.stopRefresh();
        exListView.stopLoadMore();
        if (response.getUrlParam() == UrlParam.getVideoChatUsers) {
            setChatData(response);
        }
    }

    private void setChatData(HttpResponse response) {
        // 请求失败
        if (!response.isOk()) {
            if (infos.size() != 0) {
                if (infos.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
                return;
            }
            customStatusListView.showNoData("暂无数据", "重试", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    customStatusListView.showLoading();
                    onRefresh();
                }
            });
            return;
        }

        // 非缓存
        if (!response.isCache()) {
            UserInfoLightweightList lightweightList = new UserInfoLightweightList();
            lightweightList.parseJson(response.getResponseString());

            if (lightweightList.getUserInfos().size() != 0) {

                //ref 如果是 true 并且请求的如果非第一页
                //那么返回来的就是第一页 应该把之前的数据都清掉，把返回的数据作为第一页
                if (page == 1 || lightweightList.isRef()) {
                    if (infos.size() != 0) {
                        infos.clear();
                    }
                    page = 1;
                }

                infos.addAll(lightweightList.getUserInfos());
                if (infos.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
            } else {
                if (page == 1) {
                    customStatusListView.showNoData("暂无数据", "重试", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            customStatusListView.showLoading();
                            onRefresh();
                        }
                    });
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }
            return;
        }

        // 缓存
        if (page == 1) {
            UserInfoLightweightList lightweightList = new UserInfoLightweightList();
            lightweightList.parseJson(response.getResponseString());
            if (lightweightList != null && lightweightList.getUserInfos().size() != 0) {
                if (infos.size() != 0) {
                    infos.clear();
                }
                infos.addAll(lightweightList.getUserInfos());
                if (infos.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
            }
        }
    }

    /**
     * 刷新首页
     */
    public void refreshList() {
        onRefresh();
        if (adapter != null && adapter.getList() != null && adapter.getList().size() != 0) {
            exListView.setSelection(0);
        }
    }
}
